#pragma once


#include<iostream>

using namespace std;

enum Colour
{
	RED,
	BLACK
};

template<class T>
struct RBTreeNode
{
	T _data;
	RBTreeNode<T>* _left;
	RBTreeNode<T>* _right;
	RBTreeNode<T>* _parent;
	Colour _col;

	RBTreeNode(const T& data)
		:_data(data)
		, _left(nullptr)
		, _right(nullptr)
		, _parent(nullptr)
	{ }
};

template<class T>
struct __TreeIterator
{
	typedef RBTreeNode<T> Node;
	typedef __TreeIterator<T> Self;
	Node* _node;

	__TreeIterator(Node* node)
		:_node(node)
	{ }

	T& operator*()
	{
		return _node->_data;
	}

	T* operator->()
	{
		return &_node->_data;
	}

	Self& operator++()
	{
		if (_node->_right)
		{
			_node = _node->_right;
			
			while (_node->_left)
			{
				_node = _node->_left;
			}
		}
		else
		{
			Node* cur = _node;
			Node* parent = cur->_parent;

			while (parent && cur == parent->_right)
			{
				cur = parent;
				parent = cur->_parent;
			}

			_node = parent;
		}
		
		return *this;
	}

	Self operator++(int)
	{
		Self temp(_node);

		if (_node->_right)
		{
			_node = _node->_right;

			while (_node->_left)
			{
				_node = _node->_left;
			}
		}
		else
		{
			Node* cur = _node;
			Node* parent = cur->_parent;

			while (parent && cur == parent->_right)
			{
				cur = parent;
				parent = cur->_parent;
			}

			_node = parent;
		}

		return temp;
	}

	bool operator!=(const Self& s)
	{
		return s._node != _node;
	}
};

template<class K, class T, class KeyOfT>
class RBTree
{
	typedef RBTreeNode<T> Node;
public:
	typedef __TreeIterator<T> Iterator;

	Iterator Begin()
	{
		Node* leftest = _root;

		while (leftest && leftest->_left)
		{
			leftest = leftest->_left;
		}

		return Iterator(leftest);
	}

	Iterator End()
	{
		return Iterator(nullptr);
	}

	void RotateL(Node* parent)
	{
		Node* sub_right = parent->_right;
		Node* sub_right_left = sub_right->_left;

		parent->_right = sub_right_left;
		if (sub_right_left)
		{
			sub_right_left->_parent = parent;
		}

		Node* parent_parent = parent->_parent;

		sub_right->_left = parent;
		parent->_parent = sub_right;

		if (parent == _root)
		{
			_root = sub_right;
			sub_right->_parent = nullptr;
		}
		else
		{
			if (parent == parent_parent->_left)
			{
				parent_parent->_left = sub_right;
			}
			else
			{
				parent_parent->_right = sub_right;
			}

			sub_right->_parent = parent_parent;
		}
	}

	void RotateR(Node* parent)
	{
		Node* sub_left = parent->_left;
		Node* sub_left_right = sub_left->_right;

		parent->_left = sub_left_right;
		if (sub_left_right)
		{
			sub_left_right->_parent = parent;
		}

		Node* parent_parent = parent->_parent;

		sub_left->_right = parent;
		parent->_parent = sub_left;

		if (parent == _root)
		{
			_root = sub_left;
			sub_left->_parent = nullptr;
		}
		else
		{
			if (parent == parent_parent->_left)
			{
				parent_parent->_left = sub_left;
			}
			else
			{
				parent_parent->_right = sub_left;
			}

			sub_left->_parent = parent_parent;
		}
	}

	bool insert(const T& data)
	{
		if (_root == nullptr)
		{
			_root = new Node(data);
			_root->_col = BLACK;
			return true;
		}

		Node* cur = _root;
		Node* parent = nullptr;

		KeyOfT kot;

		while (cur)
		{
			if (kot(data) > kot(cur->_data))
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (kot(data) < kot(cur->_data))
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return false;
			}
		}

		cur = new Node(data);
		cur->_col = RED;
		if (kot(data) > kot(parent->_data))
		{
			parent->_right = cur;
		}
		else
		{
			parent->_left = cur;
		}
		cur->_parent = parent;

		while (parent && parent->_col == RED)
		{
			Node* grandfather = parent->_parent;
			if (parent == grandfather->_left)
			{
				Node* uncle = grandfather->_right;
				if (uncle && uncle->_col == RED)
				{
					uncle->_col = parent->_col = BLACK;
					grandfather->_col = RED;

					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
					if (cur == parent->_left)
					{
						RotateR(grandfather);

						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
						RotateL(parent);
						RotateR(grandfather);

						cur->_col = BLACK;
						grandfather->_col = RED;
					}

					break;
				}
			}
			else
			{
				Node* uncle = grandfather->_left;
				if (uncle && uncle->_col == RED)
				{
					uncle->_col = parent->_col = BLACK;
					grandfather->_col = RED;

					cur = grandfather;
					parent = cur->_parent;
				}
				else
				{
					if (cur == parent->_left)
					{
						RotateR(parent);
						RotateL(grandfather);

						cur->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
						RotateL(grandfather);

						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					break;
				}
			}
		}

		_root->_col = BLACK;

		return true;
	}

	void _InOrder(Node* root)
	{
		if (root == nullptr)
		{
			return;
		}

		_InOrder(root->_left);
		cout << root->_kv.first << ' ';
		_InOrder(root->_right);
	}

	void InOrder()
	{
		_InOrder(_root);
		cout << endl;
	}

	bool Check(Node* root, int black_count, int count)
	{
		if (root == nullptr)
		{
			if (black_count != count)
			{
				return false;
			}

			return true;
		}

		if (root->_col == RED && root->_parent && root->_parent->_col == RED)
		{
			return false;
		}

		if (root->_col == BLACK)
		{
			black_count++;
		}

		return Check(root->_left, black_count, count)
			&& Check(root->_right, black_count, count);
	}

	bool IsBalanceTree()
	{
		if (_root == nullptr)
		{
			return true;
		}

		if (_root->_col == RED)
		{
			return false;
		}

		int count = 0;
		Node* cur = _root;
		while (cur)
		{
			if (cur->_col == BLACK)
			{
				count++;
			}

			cur = cur->_left;
		}

		return Check(_root, 0, count);
	}

	bool Find(const T& key)
	{
		Node* cur = _root;

		KeyOfT kot;

		while (cur)
		{
			if (key > kot(cur->_data))
			{
				cur = cur->_right;
			}
			else if (key < kot(cur->_data))
			{
				cur = cur->_left;
			}
			else
			{
				return true;
			}
		}

		return false;
	}

private:
	Node* _root = nullptr;
};